package com.hd.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hd.appcommon.entity.vo.OpenRecordVo;
import com.hd.appcommon.service.OpenRecordService;
import com.hd.hdxgddcommon.controller.AbstractController;
import com.hd.hdxgddcommon.entity.dto.ResultDto;
import com.hd.hdxgddcommon.page.MyPage;
import com.hd.hdxgddcommon.utils.StringUtils;
import com.hd.manager.util.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName:    entrance_guard 
 * @package:        com.hd.manager.controller
 * @className:      OpenRecordController
 * @author:     otto
 * @description:  
 * @date:    2023/7/27 15:50
 * @version:    1.0
 */ 

@RestController
@RequestMapping("/openRecord")
public class OpenRecordController extends AbstractController {
    @Autowired
    private OpenRecordService openRecordService;
    @Autowired
    private ImageUtil imageUtil;
    @RequestMapping("/getOpenRecord")
    public ResultDto getOpenRecord(MyPage page, String userName, String roomName, String buildingName){
        QueryWrapper<OpenRecordVo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(userName))
        queryWrapper.like("user_name","%"+userName+"%");
        if(StringUtils.isNotBlank(roomName))
        queryWrapper.like("room_name","%"+roomName+"%");
        if(StringUtils.isNotBlank(buildingName))
        queryWrapper.like("building_name","%"+buildingName+"%");
        queryWrapper.orderByDesc("open_time");
        IPage<OpenRecordVo> openRecordIPage = openRecordService.getOpenRecordPage(page, queryWrapper);
        List<OpenRecordVo> openRecordVoList = openRecordIPage.getRecords();
        openRecordVoList = openRecordVoList.stream().map(r->{
            if(StringUtils.isNotBlank(r.getFacePicturePath())){
                r.setFaceImage(getFaceRecognitionImage(r.getFacePicturePath()));
            }
            return r;
        }).collect(Collectors.toList());
        openRecordIPage.setRecords(openRecordVoList);
        return success(openRecordIPage);
    }

    public String getFaceRecognitionImage(String filePath){
        try {
            //filePath:图片完整路径
            String base64 = imageUtil.fileToBase64(filePath);
            return base64;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
